package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrderDao;
import com.foreknow.elm.po.*;
import com.foreknow.elm.util.CommonUtil;
import com.foreknow.elm.util.DBUtil;

import javax.xml.crypto.Data;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/15 14:03
 */
public class OrderDaoImpl implements OrderDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public int createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) throws Exception {
        int isRight=0;
        String sql="INSERT into orders(userId,businessId,daId,orderTotal,orderDate,orderState) VALUES(?,?,?,?,?,0)";

        try {
           con=DBUtil.getConnection();
           pst=con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
           pst.setString(1,userId);
           pst.setInt(2,businessId);
           pst.setInt(3,daId);
           pst.setDouble(4,orderTotal);
           pst.setString(5, CommonUtil.getCurrentDate());
           isRight= pst.executeUpdate();
           //获取自动增长列的值（一行一列）
           rs= pst.getGeneratedKeys();
           if (rs.next()){
               isRight=rs.getInt(1);
           }
        }finally {
           DBUtil.close(rs,pst);
       }
        return isRight;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception{
        Orders orders = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("b.businessId bbusinessId, ");
        sql.append("b.businessName bbusinessName, ");
        sql.append("b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId ");
        sql.append(" where o.orderId=? ");
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1, orderId);
            rs = pst.executeQuery();
            if(rs.next()){
                orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                orders.setBusiness(business);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return orders;
 }


    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        List<Orders> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        b.businessId bbusinessId, ");
        sql.append("        b.businessName bbusinessName, ");
        sql.append("        b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId ");
        sql.append(" where o.userId=?");
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql.toString());
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while(rs.next()){
                Orders o = new Orders();
                o.setOrderId(rs.getInt("orderId"));
                o.setUserId(rs.getString("userId"));
                o.setBusinessId(rs.getInt("businessId"));
                o.setOrderDate(rs.getString("orderDate"));
                o.setOrderTotal(rs.getDouble("orderTotal"));
                o.setDaId(rs.getInt("daId"));
                o.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                o.setBusiness(business);
                list.add(o);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
